package agentspeak.editors;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextDoubleClickStrategy;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.reconciler.IReconciler;
import org.eclipse.jface.text.reconciler.MonoReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;

/*
 * Métodos
 * getConfiguredContentTypes (TEM)
 * getPresentationReconciler (TEM)
 * getReconciler (TEM)
 * getContentAssistant (TEM)
 * getHyperlinkDetectors
 * getASLScanner (TEM)
 * getASLPlanScanner (TEM)
 * */
public class ASLConfiguration extends SourceViewerConfiguration {
	private ASLPlanScanner planScanner;
	private ASLScanner scanner;
	private ColorManager colorManager;
	private AgentSpeakEditor editor;

	public ASLConfiguration(ColorManager colorManager) {
		this.colorManager = colorManager;
	}
	public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
		return new String[] {
			IDocument.DEFAULT_CONTENT_TYPE,
			ASLPartitionScanner.ASL_COMMENT_MULTILINE,
			ASLPartitionScanner.ASL_COMMENT, 
			ASLPartitionScanner.ASL_INIT_DESIRE,
			ASLPartitionScanner.ASL_PLAN,
			ASLPartitionScanner.ASL_SYNTAX};
	}
	

	protected ASLScanner getASLScanner() {
		if (scanner == null) {
			scanner = new ASLScanner(colorManager);
			scanner.setDefaultReturnToken(
				new Token(
					new TextAttribute(
						colorManager.getColor(IASLColorConstants.DEFAULT))));
		}
		return scanner;
	}
	
	protected ASLPlanScanner getASLPlanScanner() {
	if (planScanner == null) {
		planScanner = new ASLPlanScanner(colorManager);
		planScanner.setDefaultReturnToken(
			new Token(
				new TextAttribute(
					colorManager.getColor(IASLColorConstants.ASL_PLAN))));
	}
	return planScanner;
}

//	public ITextDoubleClickStrategy getDoubleClickStrategy(
//			ISourceViewer sourceViewer,
//			String contentType) {
//			if (doubleClickStrategy == null)
//				doubleClickStrategy = new XMLDoubleClickStrategy();
//			return doubleClickStrategy;
//		}
	
	public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
		PresentationReconciler reconciler = new PresentationReconciler();

		DefaultDamagerRepairer dr =

		new DefaultDamagerRepairer(getASLPlanScanner());
		reconciler.setDamager(dr, ASLPartitionScanner.ASL_PLAN);
		reconciler.setRepairer(dr, ASLPartitionScanner.ASL_PLAN);
				
		dr = new DefaultDamagerRepairer(getASLScanner());
		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);

		NonRuleBasedDamagerRepairer ndrCommentMultiline =
			new NonRuleBasedDamagerRepairer(
				new TextAttribute(
					colorManager.getColor(IASLColorConstants.ASL_COMMENT_MULTILINE)));
		reconciler.setDamager(ndrCommentMultiline, ASLPartitionScanner.ASL_COMMENT_MULTILINE);
		reconciler.setRepairer(ndrCommentMultiline, ASLPartitionScanner.ASL_COMMENT_MULTILINE);
		
		NonRuleBasedDamagerRepairer ndrComment =
				new NonRuleBasedDamagerRepairer(
					new TextAttribute(
						colorManager.getColor(IASLColorConstants.ASL_COMMENT)));
			reconciler.setDamager(ndrComment, ASLPartitionScanner.ASL_COMMENT);
			reconciler.setRepairer(ndrComment, ASLPartitionScanner.ASL_COMMENT);
			
		NonRuleBasedDamagerRepairer ndrInitDesire =
				new NonRuleBasedDamagerRepairer(
					new TextAttribute(
						colorManager.getColor(IASLColorConstants.ASL_INIT_DESIRE)));
			reconciler.setDamager(ndrInitDesire, ASLPartitionScanner.ASL_INIT_DESIRE);
			reconciler.setRepairer(ndrInitDesire, ASLPartitionScanner.ASL_INIT_DESIRE);
		
		NonRuleBasedDamagerRepairer ndr7 =
				new NonRuleBasedDamagerRepairer(
					new TextAttribute(
						colorManager.getColor(IASLColorConstants.SYNTAX)));
			reconciler.setDamager(ndr7, ASLPartitionScanner.ASL_SYNTAX);
			reconciler.setRepairer(ndr7, ASLPartitionScanner.ASL_SYNTAX);	

		return reconciler;
	}

    public IReconciler getReconciler(){
        ASLReconcilingStrategy strategy = new ASLReconcilingStrategy();
        strategy.setEditor(this.editor);
        
        MonoReconciler reconciler = new MonoReconciler(strategy,false);
        
        return reconciler;
    }
    
    public IContentAssistant getContentAssistant(ISourceViewer sourceViewer)
    {
        ContentAssistant assistant = new ContentAssistant();
        assistant.setContentAssistProcessor(new TagContentAssistProcessor(),IDocument.DEFAULT_CONTENT_TYPE);
        assistant.enableAutoActivation(true);
        
        
        return assistant;
    }
}